package com.hannesdorfmann.mosby3.sample.mvi.view.home;

import com.hannesdorfmann.mosby3.sample.mvi.EventHelper;
import com.hannesdorfmann.mosby3.sample.mvi.MainAbility;
import com.hannesdorfmann.mosby3.sample.mvi.ResourceTable;
import com.hannesdorfmann.mosby3.sample.mvi.TestUtils;
import com.hannesdorfmann.mosby3.sample.mvi.businesslogic.model.FeedItem;
import com.hannesdorfmann.mosby3.sample.mvi.view.widge.CirleProgressImg;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.agp.components.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.List;
import java.util.Optional;

import static org.junit.Assert.*;

public class HomeFractionTest {

    MainAbility slice;
    @Before
    public void setUp() throws Exception {
        slice = EventHelper.startAbility(MainAbility.class);
    }

    @After
    public void tearDown() throws Exception {
        EventHelper.clearAbilities();
        Thread.sleep(2000);
    }

    @Test
    public void onProductClicked() {
//        MainAbility slice = EventHelper.startAbility(MainAbility.class);
        TestUtils.sleep(2000);
        assertNotNull(slice);

        Optional<Fraction> fractionOptional = slice.getFractionManager().getFractionByTag(HomeFraction.class.getSimpleName());
        HomeFraction fraction;
        if(fractionOptional.isPresent()){
            fraction = (HomeFraction) fractionOptional.get();
            TestUtils.sleep(1000);
//            Id_fragmentContainer
            ComponentContainer container = (ComponentContainer) slice.findComponentById(ResourceTable.Id_fragmentContainer);
            assertNotNull(container);
            ListContainer listContainer = (ListContainer) container.findComponentById(ResourceTable.Id_listContainer);
            assertNotNull(listContainer);

            HomeAdapter adapter = (HomeAdapter) listContainer.getItemProvider();
            assertNotNull(adapter);

            TestUtils.sleep(8000);

            CirleProgressImg loadingView = (CirleProgressImg) container.findComponentById(ResourceTable.Id_loadingView);
            do{
                TestUtils.sleep(2000);
            }while(loadingView.getVisibility() == Component.VISIBLE);

            List<FeedItem> list =  adapter.getItems();
            Text errorView = (Text) slice.findComponentById(ResourceTable.Id_errorView);
            if(errorView.getVisibility() == Component.VISIBLE){
                assertTrue(list == null || list.size() == 0);
            }else{
                assertTrue(list != null && list.size() > 0);
            }

        }
    }

}